
package com.platform.modules.sys.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.platform.common.annotation.SysLog;
import com.platform.common.exception.BusinessException;
import com.platform.common.utils.*;
import com.platform.common.validator.AbstractAssert;
import com.platform.common.validator.ValidatorUtils;
import com.platform.common.validator.group.AddGroup;
import com.platform.common.validator.group.UpdateGroup;
import com.platform.modules.sys.entity.SysOrgEntity;
import com.platform.modules.sys.entity.SysSmsLogEntity;
import com.platform.modules.sys.entity.SysUserEntity;
import com.platform.modules.sys.form.PasswordForm;
import com.platform.modules.sys.service.SysOrgService;
import com.platform.modules.sys.service.SysSmsLogService;
import com.platform.modules.sys.service.SysUserRoleService;
import com.platform.modules.sys.service.SysUserService;
import org.apache.commons.lang.ArrayUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.crypto.hash.Sha256Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 系统用户
 *
 * @author
 */
@RestController
@RequestMapping("/sys/user")
public class SysUserController extends AbstractController {
    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private SysUserRoleService sysUserRoleService;
    @Autowired
    private SysOrgService sysOrgService;

    @Autowired
    private SysSmsLogService sysSmsLogService;
    @Autowired
    private JedisUtil jedisUtil;

    /**
     * 查看所有列表
     *
     * @param params 查询参数
     * @return RestResponse
     */
    @RequestMapping("/queryAll")
    @RequiresPermissions("sys:dict:list")
    public RestResponse queryAll(@RequestParam Map<String, Object> params) {
        List<SysUserEntity> list = sysUserService.queryAll(params);

        return RestResponse.success().put("list", list);
    }

    /**
     * 所有用户列表
     *
     * @param params 查询参数
     * @return RestResponse
     */
    @GetMapping("/list")
    @RequiresPermissions("sys:user:list")
    public RestResponse list(@RequestParam Map<String, Object> params) {

        //如需数据权限，在参数中添加DataScope
        params.put("dataScope", getDataScope());

        Page page = sysUserService.queryPage(params);

        return RestResponse.success().put("page", page);
    }

    /**
     * 获取登录的用户信息
     *
     * @return RestResponse
     */
    @GetMapping("/info")
    public RestResponse info() {
        SysOrgEntity orgEntity = sysOrgService.getById(getUser().getOrgNo());

        return RestResponse.success().put("user", getUser()).put("org", orgEntity);
    }

    /**
     * 修改登录用户密码
     *
     * @param form form
     * @return RestResponse
     */
    @SysLog("修改密码")
    @PostMapping("/password")
    public RestResponse password(@RequestBody PasswordForm form) {
        AbstractAssert.isBlank(form.getNewPassword(), "新密码不为能空");

        //sha256加密
        String password = new Sha256Hash(form.getPassword(), getUser().getSalt()).toHex();
        //sha256加密
        String newPassword = new Sha256Hash(form.getNewPassword(), getUser().getSalt()).toHex();

        //更新密码
        boolean flag = sysUserService.updatePassword(getUserId(), password, newPassword);
        if (!flag) {
            return RestResponse.error("原密码不正确");
        }
        return RestResponse.success();
    }

    /**
     * 登录用户手机号
     *
     * @param form form
     * @return RestResponse
     */
    @SysLog("登录用户手机号")
    @PostMapping("/updateMobile")
    public RestResponse updateMobile(@RequestBody PasswordForm form) {
        AbstractAssert.isBlank(form.getMobile(), "新密码不为能空!");
        AbstractAssert.isBlank(form.getSmsCode(), "验证码不能为空!");
        checkSmsCode(form.getSmsCode(), form.getMobile());

        sysUserService.lambdaUpdate().set(SysUserEntity::getMobile, form.getMobile())
                .eq(SysUserEntity::getUserId, getUserId()).update();

        return RestResponse.success();
    }


    /**
     * 根据主键查询详情
     *
     * @param userId 主键
     * @return RestResponse
     */
    @GetMapping("/info/{userId}")
    @RequiresPermissions("sys:user:info")
    public RestResponse info(@PathVariable("userId") String userId) {
        SysUserEntity user = sysUserService.getById(userId);

        //获取用户所属的角色列表
        List<String> roleIdList = sysUserRoleService.queryRoleIdList(userId);
        user.setRoleIdList(roleIdList);

        return RestResponse.success().put("user", user);
    }

    /**
     * 保存用户
     *
     * @param user user
     * @return RestResponse
     */
    @SysLog("保存用户")
    @PostMapping("/save")
    @RequiresPermissions("sys:user:save")
    public RestResponse save(@RequestBody SysUserEntity user) {
        ValidatorUtils.validateEntity(user, AddGroup.class);

        Map<String, Object> params = new HashMap<>(2);
        params.put("dataScope", getDataScope());

        user.setCreateUserId(getUserId());
        user.setCreateUserOrgNo(getOrgNo());
        sysUserService.add(user, params);

        return RestResponse.success();
    }

    /**
     * 修改用户
     *
     * @param user user
     * @return RestResponse
     */
    @SysLog("修改用户")
    @PostMapping("/update")
    @RequiresPermissions("sys:user:update")
    public RestResponse update(@RequestBody SysUserEntity user) {
        ValidatorUtils.validateEntity(user, UpdateGroup.class);

        Map<String, Object> params = new HashMap<>(2);
        params.put("dataScope", getDataScope());

        user.setCreateUserId(getUserId());
        user.setCreateUserOrgNo(getOrgNo());
        sysUserService.update(user, params);

        return RestResponse.success();
    }

    /**
     * 删除用户
     *
     * @param userIds userIds
     * @return RestResponse
     */
    @SysLog("删除用户")
    @PostMapping("/delete")
    @RequiresPermissions("sys:user:delete")
    public RestResponse delete(@RequestBody String[] userIds) {
        if (ArrayUtils.contains(userIds, Constant.SUPER_ADMIN)) {
            return RestResponse.error("系统管理员不能删除");
        }

        if (ArrayUtils.contains(userIds, getUserId())) {
            return RestResponse.error("当前用户不能删除");
        }

        sysUserService.deleteBatch(userIds);

        return RestResponse.success();
    }

    /**
     * 重置密码
     *
     * @param userIds userIds
     * @return RestResponse
     */
    @SysLog("重置密码")
    @PostMapping("/resetPw")
    @RequiresPermissions("sys:user:resetPw")
    public RestResponse resetPw(@RequestBody String[] userIds) {
        if (ArrayUtils.contains(userIds, Constant.SUPER_ADMIN)) {
            return RestResponse.error("系统管理员不能重置");
        }

        if (ArrayUtils.contains(userIds, getUserId())) {
            return RestResponse.error("当前用户不能重置");
        }

        sysUserService.resetPw(userIds);

        return RestResponse.success();
    }

    @PostMapping("/getSmsCode")
    public RestResponse getSmsCode(@RequestBody PasswordForm form){
        String mobile = form.getMobile();
        if (StringUtils.isBlank(mobile) || mobile.length() != 11) {
            return RestResponse.error("手机号码格式不对");
        }
        String code = jedisUtil.get(Constant.SYS_SMS + mobile);
        // 一分钟之内不能重复发送短信
        String stint = jedisUtil.get(Constant.SYS_SMS + mobile);
        String smsCode;
        if (StringUtils.isNotBlank(stint)) {
            return RestResponse.success("短信已发送");
        } else {
            if (StringUtils.isNotBlank(code)) {
                smsCode = code;
            } else {
                //生成验证码
                smsCode = CharUtil.getRandomNum(6);
            }
        }
        SysSmsLogEntity smsLog = new SysSmsLogEntity();
        JSONObject js = new JSONObject();
        js.put("code", smsCode);
        smsLog.setContent(js.toJSONString());
        smsLog.setCode(smsCode);
        smsLog.setMobile(mobile);
//        sysSmsLogService.sendSms(userEntity.getId(), smsLog, templateId);
        sysSmsLogService.sendSms(getUserId(), smsLog);
        //发送成功之后将验证码存入redis，有效期为5分钟
        jedisUtil.set(Constant.SYS_SMS + mobile, smsCode, 300);
        jedisUtil.set(Constant.SYS_VERIFICATIONSNUMBER + 1, smsCode, 300);
        return RestResponse.success("短信已发送");
    }

    public void checkSmsCode(String code, String mobile) {
        String smsCode = jedisUtil.get(Constant.SYS_SMS + mobile);
        if (org.apache.commons.lang3.StringUtils.isBlank(smsCode)) {
            throw new BusinessException("短信验证码错误");
        } else {
            if (StringUtils.isNullOrEmpty(smsCode)) {
                throw new BusinessException("短信验证码已失效，请重新获取");
            }
            if (!code.equals(smsCode)) {
                Integer vNumber;
                if (StringUtils.isBlank(jedisUtil.get(Constant.SYS_VERIFICATIONSNUMBER + mobile))) {
                    vNumber = 0;
                } else {
                    vNumber = Integer.valueOf(jedisUtil.get(Constant.SYS_VERIFICATIONSNUMBER + mobile));
                }
                if (vNumber > 5) {
                    jedisUtil.del(Constant.SYS_SMS + mobile);
                    jedisUtil.del(Constant.SYS_VERIFICATIONSNUMBER + mobile);
                } else {
                    vNumber++;
                    jedisUtil.set(Constant.SYS_VERIFICATIONSNUMBER + mobile, String.valueOf(vNumber), 300);
                }
                throw new BusinessException("短信验证码错误");
            }

        }
    }

}
